<?php header("Content-Type:text/html;   charset=utf-8"); ?>
<script type="text/javascript">
	$(function(){
		$("a[href='#maximize']").trigger("click");
		$("#txtTitleNewSms").bind("blur",checkTitleNewSms);
		$("#txtContentNewSms").bind("blur",checkContentNewSms);
		$("#txtUserNameNewSms").bind("blur",checkGroupNewSms);
		$("#btnSubmitNewSms").bind("click",submitSms);
		loadSmsNewSms();
	});	
	function submitSms(){		
		if(checkTitleNewSms()==false)
			return;
		if(checkContentNewSms()==false)
			return;
		if(checkGroupNewSms()==false)
			return;		
		var title = $("#txtTitleNewSms").val();
		
		var content = $("#txtContentNewSms").val();
		
		var gid = getSelectedGroupId();
		
		var username = $("#txtUserNameNewSms").val();
		
		var remark = $("#txtRemarkNewSms").val();
		
		var params = {
					title:title,
					content:content,
					groupidstr:gid,
					username:username,
					remark:remark
				};	
		
		$.ajax({
			type:"post",
			url:"admin.php/manage/addSms",
			dataType:"json",
			data:params,
			success:function(data){
				parent.loadSms();
				alert("提交成功");
				$("#btnCloseNewSms").trigger("click");
			}
		});
	}
	
	function loadSmsNewSms(){
		var id = $("#hdidsms").val();
		if(id==0)
			return;
		$("input,textarea").attr("disabled","disabled");
		$("div.buttonActive").hide();
		$("div.button").attr("style","margin-left:260px;")
		var params = {id:id};
		$.ajax({
			type:"post",
			url:"admin.php/manage/getSmsById",
			dataType:"json",
			data:params,
			success:function(data){
				
				$("#txtTitleNewSms").val(data.title);
				$("#txtContentNewSms").val(data.content);
				$("#txtRemarkNewSms").val(data.remark);
				$("input[type='checkbox'][id^='chkGroup']").each(function(){
					var v = "/"+$(this).val()+"/";
					if(data.groupidstr.indexOf(v)!=-1)
						$(this).attr("checked",true);
				});
				$("#txtUserNameNewSms").val(data.username);				
			}
		});
	}
	function checkTitleNewSms(){
		var title = $("#txtTitleNewSms").val();
		if(IsEmpty(title)){
			SetError("txtTitleNewSms","主题不能为空");
			return false;
		}
		var len = title.length;
		if(len>100){
			SetError("txtTitleNewSms","最多100字");
			return false;
		}
		SetOK("txtTitleNewSms");
		return true;
	}
	function checkContentNewSms(){
		var content = $("#txtContentNewSms").val();
		if(IsEmpty(content)){
			SetError("txtContentNewSms","内容不能为空");
			return false;
		}		
		SetOK("txtContentNewSms");
		return true;
	}
	function checkGroupNewSms(){
		var username = $("#txtUserNameNewSms").val();
		var gid = getSelectedGroupId();		
		if(IsEmpty(username) && IsEmpty(gid)){
			SetError("txtUserNameNewSms","分组和自定义发送不能同时为空");
			return false;
		}		
		SetOK("txtUserNameNewSms");
		return true;
	}
	function getSelectedGroupId(){
		var ids = "";
		$("input[type='checkbox'][id^='chkGroup']:checked").each(function(){
			ids+=$(this).val()+"/";
		});
		return ids;
	}
</script>
<div class="pageContent">
	<form class="pageForm required-validate">		
		<div class="pageFormContent" style="; width: 98%;"
			layoutH="56">
			<table class="">
				<tr>
					<td>消息标题：</td>
					<td><input id="txtTitleNewSms" type="text" style="width:500px" value="" /></td>
				</tr>
				<tr>
					<td style='line-height: 130px'>消息内容：</td>
					<td><textarea id="txtContentNewSms" style="width:500px;height:100px" rows="" cols=""></textarea></td>
				</tr>
				<tr>
					<td>分组发送：</td>
					<td style="text-align: left">
					<?php foreach ($group as $key=>$value){
						print "<input type='checkbox' style='vertical-align:middle' id='chkGroup$key' value='$key' /><span style='clear:both;vertical-align:middle;' for='chkGroup$key'>$value</span>";
					}?>
					</td>
				</tr>
				<tr>
					<td style='line-height: 130px'>自定义发送：</td>
					<td><textarea id="txtUserNameNewSms" style="width:500px;height:100px" rows="" cols=""></textarea><label>【填写接收消息的用户名，用半角逗号隔开】</label></td>
				</tr>
				<tr>
					<td>备注：</td>
					<td style='line-height: 130px'>
					<textarea id="txtRemarkNewSms" style="width:500px;height:100px" rows="" cols=""></textarea>
					</td>
				</tr>
				
				<tr>
					<td colspan="2" style='line-height: 40px'>&nbsp;&nbsp;</td>
				</tr>
			</table>
			<input type="hidden" id="hdidsms"
				value="<?php if(isset($_GET["id"])) print $_GET["id"]; else print 0;?>">

			<div class="buttonActive" style="width: 120px; margin-left: 180px;">
				<div class="buttonContent">
					<button id="btnSubmitNewSms" type="button" style="width: 80px">提 交</button>
				</div>
			</div>

			<div class="button" style="text-align:right">
				<div class="buttonContent" style="text-align: right">
					<button id="btnCloseNewSms" type="button" class="close"
						style="width: 80px">取 消</button>
				</div>
			</div>
		</div>		
	</form>
</div>
